package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _58两数相加
 * @description
 * @date 2023/07/27 15:53
 * <p>
 * ******************************************************
 */
public class _58两数相加 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode p = dummy;
        int next = 0;
        while ((l1 != null || l2 != null) || next != 0) {
            int l1Value = 0;
            int l2Value = 0;
            if (l1 != null) {
                l1Value = l1.val;
                l1 = l1.next;
            }

            if (l2 != null) {
                l2Value = l2.val;
                l2 = l2.next;
            }
            int value = l1Value + l2Value + next;
            next = value / 10;
            int currValue = value % 10;
            p.next = new ListNode(currValue);
            p = p.next;
        }
        return dummy.next;
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
